package com.proj.dao;

import com.proj.model.ResultModel;
import com.proj.model.SearchGoods;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品查询Dao
 *
 * 
 */
@Repository
public class SearchDao {

  public ResultModel search(SolrQuery query) throws Exception {
    String solrUrl = "http://8.130.171.48:8983/solr/shuxiang_core";

    HttpSolrClient solrClient =
        new HttpSolrClient.Builder(solrUrl)
            .withConnectionTimeout(10000)
            .withSocketTimeout(6000)
            .build();

    // 执行查询
    QueryResponse response = solrClient.query(query);

    // 获取查询结果
    SolrDocumentList solrDocumentList = response.getResults();

    // 获取查询结果总记录数
    ResultModel resultModel = new ResultModel();
    long numFound;
    numFound = solrDocumentList.getNumFound();
    resultModel.setRecordCound(numFound);

    // 商品列表
    List<SearchGoods> searchGoodsList = new ArrayList<SearchGoods>();

    // 获取查询结果集
    for (SolrDocument solrDocument : solrDocumentList) {
      // 创建一个商品对象
      SearchGoods searchGoods = new SearchGoods();
      searchGoods.setId((String) solrDocument.get("product_id"));
      searchGoods.setName((String) solrDocument.get("product_name"));
      searchGoods.setCategoryId((int) solrDocument.get("type_id"));
      searchGoods.setISBN((String) solrDocument.get("product_ISBN"));
      searchGoods.setPrice((Float) solrDocument.get("product_price"));
      searchGoods.setImageURL((String) solrDocument.get("product_image_url"));
      searchGoods.setAppearance((String) solrDocument.get("product_grade"));
      searchGoods.setAuthor((String) solrDocument.get("product_writer"));
      searchGoods.setTranslator((String) solrDocument.get("product_translator"));
      searchGoods.setVersion((String) solrDocument.get("product_version"));
      searchGoods.setStore((int) solrDocument.get("product_stock"));
      searchGoods.setStatus((int) solrDocument.get("product_status"));
      searchGoods.setFreight((Float) solrDocument.get("product_freight"));
      searchGoods.setPressHorse((String) solrDocument.get("product_press"));
      searchGoods.setPublicationDate((String) solrDocument.get("product_publication_date"));
      searchGoods.setCreatetime((String) solrDocument.get("product_createtime"));
      searchGoods.setUpdateDate((String) solrDocument.get("product_updatetime"));
      searchGoods.setViewCount((int) solrDocument.get("product_visit"));
      searchGoods.setDescribe((String) solrDocument.get("product_describe"));
      searchGoods.setUserId((String) solrDocument.get("user_id"));

      // 取高亮显示
      Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
      List<String> list = highlighting.get(solrDocument.getFieldValue("id")).get("product_name");
      System.out.println(list);
      String goodsName = "";
      if (list != null && list.size() > 0) {
        goodsName = list.get(0);
      } else {
        goodsName = (String) solrDocument.get("goodsName");
      }
      searchGoods.setStyleName(goodsName);
      searchGoodsList.add(searchGoods);
    }

    // 添加到返回结果
    resultModel.setSearchGoodsList(searchGoodsList);
    return resultModel;
  }
}
